var app = getApp();
const stateOptions = [{
  name: "全部",
  value: ''
}, {
  name: "正常",
  value: 1
}, {
  name: "注销",
  value: 2
}];
const pageSize = 20;
Page({
  data: {
    page: 1, //当前页数
    tplList: [],
    loadmore: false,
    nomore: false,
    isFilter: false,
    drawer: false,
    conditions: {
      stateIndex: "",
      full_name: "",
      tel: "",
    },
    stateOptions,
    empty: false,
  },

  onLoad: function (options) {
    this.salesmanList();
  },



  onPullDownRefresh: function () {
    this.reloadData();
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.salesmanList();
    }
  },

  reloadData() {
    this.setData({
      page: 1,
      tplList: [],
      loadmore: false,
      nomore: false,
    })
    wx.showLoading();
    this.salesmanList();
  },


  formatIdCard(idCard) {
    return idCard.replace(/^(.{6})(?:\d+)(.{4})$/, "$1****$2");
  },




  //业务员列表
  salesmanList() {
    app.ajax({
      url: 'Partner/Salesman/salesmanList',
      data: {
        page: this.data.page,
        row: pageSize,
        full_name: this.data.conditions.full_name,
        tel: this.data.conditions.tel,
        state: this.data.conditions.stateIndex !== '' ? this.data.stateOptions[this.data.conditions.stateIndex].value : ''
      },
      success: (res) => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.data.code > 0) {
          const source = res.data.data;
          source.data.forEach(current => {
            return current.id_card = this.formatIdCard(current.id_card)
          })
          let list = this.data.tplList.concat(source.data);
          this.setData({
            tplList: list,
            empty: source.page.total == 0 ? true : false,
            loadmore: source.page.total > list.length ? true : false,
            nomore: source.page.total <= list.length ? true : false,
            drawer:false
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误");
      }
    })
  },
  //添加业务员
  addFranchiseee() {
    wx.navigateTo({
      url: '../salesmanListEdit/salesmanListEdit?type=1',
    })
  },
  //编辑业务员
  editFranchiseee(e) {
    wx.navigateTo({
      url: '../salesmanListEdit/salesmanListEdit?type=2&s_id=' + e.currentTarget.dataset.s_id,
    })
  },
  //查看业务员
  lookFranchiseee(e) {
    // console.log(e.currentTarget.dataset.s_id)
    wx.navigateTo({
      url: '../salesmanListEdit/salesmanListEdit?type=3&s_id=' + e.currentTarget.dataset.s_id,
    })
  },
  //删除业务员
  deleteFranchiseee(e) {
    var that = this;
    wx.showModal({
      content: '是否删除销售?',
      confirmText: '是',
      cancelText: '否',
      success: function (res) {
        //console.log(res)
        if (res.confirm) {
          app.ajax({
            url: 'Partner/Salesman/deleteSalesman',
            method: "POST",
            data: {
              id: e.currentTarget.dataset.s_id
            },
            success: function (res) {
              if (res.data.code == 1000) {
                app.showToast(res.data.message, "success");
                setTimeout(() => {
                  that.reloadData();
                }, 1500)
              } else {
                app.showToast(res.data.message);
                return false;
              }
            }
          })
        }
      }
    })
  },

  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },



  handleChange(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    this.setData({
      [`conditions.${name}`]: index
    })
  },



  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },


 
})